package com.walter.learn.Util;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

/**
 * @ProjectName: learnDemo
 * @Package: com.walter.util
 * @ClassName: GetCurrentProcessInfo
 * @Author: DELL
 * @Description: ${description}
 * @Date: 2019/8/2 19:56
 * @Version: 1.0
 */

/**
 * @Description 获取当前进程的信息
 * @Create 2019/8/2 20:05
 * @Author wangzheng
 */
public class GetCurrentProcessInfo {
    private static RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();

    /**
     * @Description 获取当前进程名
     */
    public static String getProcessName() {
        return runtimeMXBean.getName();
    }

    /**
     * @Description 获取当前进程PID
     */
    public static int getProcessID() {
        String name = runtimeMXBean.getName();
        int index = name.indexOf("@");
        if (index != -1) {
            int pid = Integer.parseInt(name.substring(0, index));
            return pid;
        }
        return -1;
    }
}
